﻿using System;
using System.Web.UI;
using Business;

public partial class Controls_BackEnd_RegionEdit : UserControl
{

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack) return;
        GetData();
    }


    /// <summary>
    /// 
    /// </summary>
    private void GetData()
    {
        Region = Config.Id > 0 ? new Region { ReID = Config.Id }.Region_GetByReID() : new Region();
        CheckPermission();

    }


    /// <summary>
    /// 
    /// </summary>
    private void CheckPermission()
    {

        var permission = (UserFunctions)Session[SessionManager.SessionPermission];
        btnSave.Visible = Config.Id > 0 ? permission.IsUpdate : permission.IsInsert;
        btnReset.Visible = permission.IsInsert;
    }


    /// <summary>
    /// 
    /// </summary>
    protected Region Region
    {
        get
        {
            var region = (Region)Session[SessionManager.SessionData] ?? new Region();
            region.ReName = txtName.Text;
            region.Alias = txtAlias.Text;
            return region;
        }
        set
        {
            Session[SessionManager.SessionData] = value;
            txtAlias.Text = value.Alias;
            txtName.Text = value.ReName;
        }
    }




    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        switch (((Control)sender).ID)
        {
            case "btnReset":
                Region = new Region();
                Config.Id = 0;
                break;
            case "btnBack":
                Config.Back();
                break;
            case "btnSave":
                var error = Region.Save();
                if (error > 0)
                {
                    Region.ReID = Config.Id = error;
                    Config.Message(Page, Resources.Resource.SaveSuccess, Resources.Resource.Messenger);
                    return;
                }
                switch (error)
                {
                    case -1:
                        Config.Message(Page, string.Format(Resources.Resource.ObjectExisted, Resources.Resource.Region), Resources.Resource.Warning);
                        break;
                    default:
                        Config.Message(Page, Resources.Resource.ErrorMsg, Resources.Resource.Warning);
                        break;
                }
                break;

        }

    }
}
